#! /usr/bin/python

# getting all the functionality of the facebook api
from facebook import *

# to handle unicode
import codecs

##################################### GLOBALS #####################################

# recieving an access_token that doesn't expire via this site: http://fbrell.com/auth/offline-access-token
# we used a dedicated account for this project
OAUTH_ACCESS_TOKEN = "184484190795|387d40acb600b4421925c827-100001834274618|Yyl5-SYA_CozI5TjZ73SOV69dIs"

# the object the performs all the api interactions
GRAPH = GraphAPI(OAUTH_ACCESS_TOKEN)

# holds the fanpages we are going to query
FAN_PAGE = []

# turning the fanpages file into a list, we currently support only files that are in the same folder
# we use the diaz sign as a remark - same as in python
fanpages_file = open('fanpages','r')
fan_page_file_content = fanpages_file.read()

FAN_PAGE = fan_page_file_content.split('\n')
# getting rid of the trailing \n at the end of the list 
FAN_PAGE = FAN_PAGE[0:len(FAN_PAGE)-1]
# keeping the pages that arn't commented out
FAN_PAGE = filter(lambda str: str[0] != '#', FAN_PAGE)

# main database holds data in the following format : fan page name, id, name, type of post, post text (if exists), object id
# type of posts is one of the followning: 
# wall post, comment, like, picture text

MAIN_DB = codecs.open('main_db.txt', 'wb', encoding='utf-8')


##################################### GLOBALS #####################################

##################################### UTIL FUNCTIONS #####################################

def write_main_db(fan_page,obj_type,obj):
	
	if obj_type == 'like':
		db_str = fan_page+' '+obj['id']+' '+obj['name']+' like'
	else: 
		db_str = fan_page+' '+str(obj['from']['id'])+' '+obj['from']['name']+' '+obj_type
		try:
			db_str = db_str+' '+obj['message']
		except KeyError:
			pass
	db_str = db_str + ' '+obj['id']+'\n'	
	MAIN_DB.write(db_str)

##################################### UTIL FUNCTIONS #####################################


##################################### FUNCTIONS #####################################

def print_post(fan_page,post):
	"""
	Adding each post by the specified form to our MAIN_DB
	"""
	obj_type = 'link'	
	try:
		link = post['link']
	except KeyError:
		obj_type = 'post'
	write_main_db(fan_page,obj_type,post)

def print_comments(fan_page,comment):
	"""
	Traversing all the specified comments and adding each comment in the specified
	form to our MAIN_DB
	"""
	for comment_strct in comment['data']:
		write_main_db(fan_page,'comment',comment_strct)

def print_likes(fan_page,like):
	"""
	Traversing all the specified likes and adding each like in the specified
	form to our MAIN_DB
	"""
	for like_strct in like['data']:
		write_main_db(fan_page,'like',like_strct)

def query_elements(fan_page,connection):
	"""
	We recieve a connection title to recieve a collection of objects of the same type that we use to query.
	For each object we query the comments and likes made on it.  
	"""
	obj_list = GRAPH.get_connections(fan_page,connection)
	for obj in obj_list['data']:
		obj_id = obj['id']

		# getting all the comments for a given post
		comments = GRAPH.get_connections(obj_id,"comments")		
		# printing each comment in the format of the specified scheme 
		print_comments(fan_page,comments) 

		# getting all the likes for a goven post
		likes = GRAPH.get_connections(obj_id,"likes")
		# printing each like in the format of the specified scheme
		print_likes(fan_page,likes)


##################################### FUNCTIONS #####################################

##################################### MAIN #####################################

# retrieving page's information
for fan_page in FAN_PAGE:
	
	# collecting information on the page's wall
	query_elements(fan_page,"feed")
	# printing the posts message if exists
	posts_list = GRAPH.get_connections(fan_page,"feed")
	for post in posts_list['data']:
		post_id = post['id']
		print_post(fan_page,post)

	# collecting information on the page's photos
	query_elements(fan_page,"photos")
		
	# collecting information on the page's videos
	query_elements(fan_page,"videos")	

##################################### MAIN #####################################

